//
//  BaseEngine.m
//  HuLaQuan
//
//  Created by hok on 1/7/16.
//  Copyright © 2016 Jianghao. All rights reserved.
//

#import "BaseEngine.h"


@implementation BaseEngine



- (NSString *)formRequestUrlWithPath:(NSString *)path
{
    return [[@"http://" stringByAppendingString:SERVER_HOST_CUSTOMER] stringByAppendingString:path];
}
- (NSString *)firstString
{
 
    return [self base64EncodeString:@"898520000011111"];
   // return [self base64EncodeString:@"850000000000000"];
}
- (NSString *)secondStringWithKey:(NSString *)key
{
   // NSLog(@"1.--- %@ 密钥 --- %@",key,PUBLIC_KEY);
    return [RSA encryptString:key publicKey:PUBLIC_KEY] ;
}
- (NSString *)thirdStringWithBody:(NSString *)src andKey:(NSString *)key
{
    return [_DES encrypt:src withKey:key] ;
}


- (NSString *)getSerialNumber
{
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    NSString *timeSp = [NSString stringWithFormat:@"%d", (long)[datenow timeIntervalSince1970]];
    NSLog(@"timeSp:%@",timeSp); //时间戳的值
    return timeSp;
}
/**
 *  解析从银联后台连接成功后获取的数据
 *
 *  @param resultString xml数据
 *
 *  @return 解析后的NSDictionary
 */
- (NSDictionary *)parseXMLFromYinLianConnection:(NSString *)resultString
{
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:[resultString dataUsingEncoding:NSUTF8StringEncoding]];
    ////                   //这里使用了第三方框架 XMLDictionary，他本身继承并实现 NSXMLParserDelegate 委托代理协议，对数据进行遍历处理
    // NSLog(@"%@",[NSDictionary dictionaryWithXMLParser:parser]);
    
    NSDictionary *resultDic = [[NSDictionary alloc] initWithDictionary:[NSDictionary dictionaryWithXMLParser:parser]];
    return resultDic;
}
      // Open source，base64处理，就两个文件，自己从网上下载下吧
/*- (NSString*)TripleDES:(NSString*)plainText encryptOrDecrypt:(CCOperation)encryptOrDecrypt key:(NSString*)key {
    
    
    const void *vplainText;
    size_t plainTextBufferSize;
    
    if (encryptOrDecrypt == kCCDecrypt)
    {
        NSData *EncryptData = [GTMBase64 decodeData:[plainText dataUsingEncoding:NSUTF8StringEncoding]];
        plainTextBufferSize = [EncryptData length];
        vplainText = [EncryptData bytes];
    }
    else
    {
        NSData* data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
        plainTextBufferSize = [data length];
        vplainText = (const void *)[data bytes];
    }
    
    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;
    // uint8_t ivkCCBlockSize3DES;
    
    bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) & ~(kCCBlockSize3DES - 1);
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);
    // memset((void *) iv, 0x0, (size_t) sizeof(iv));
    
    //    NSString *key = @"123456789012345678901234";
    NSString *initVec = @"01234567";
    const void *vkey = (const void *) [key UTF8String];
    const void *vinitVec = (const void *) [initVec UTF8String];
    
    ccStatus = CCCrypt(encryptOrDecrypt,
                       kCCAlgorithm3DES,
                       kCCOptionPKCS7Padding,
                       vkey, //"123456789012345678901234", //key
                       kCCKeySize3DES,
                       vinitVec, //"init Vec", //iv,
                       vplainText, //"Your Name", //plainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);
    if (ccStatus == kCCSuccess) NSLog(@"SUCCESS");
    else if (ccStatus == kCC ParamError) return @"PARAM ERROR";
     else if (ccStatus == kCCBufferTooSmall) return @"BUFFER TOO SMALL";
     else if (ccStatus == kCCMemoryFailure) return @"MEMORY FAILURE";
     else if (ccStatus == kCCAlignmentError) return @"ALIGNMENT";
     else if (ccStatus == kCCDecodeError) return @"DECODE ERROR";
     else if (ccStatus == kCCUnimplemented) return @"UNIMPLEMENTED";
    
    NSString *result;
    
    if (encryptOrDecrypt == kCCDecrypt)
    {
        result = [[NSString alloc] initWithData:[NSData dataWithBytes:(const void *)bufferPtr
                                                                length:(NSUInteger)movedBytes]
                                        encoding:NSUTF8StringEncoding]
                  ;
    }
    else
    {
        NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
        result = [GTMBase64 stringByEncodingData:myData];
    }
    
    return result;
    
}*/
- (NSString *)base64EncodeString:(NSString *)string
{
    NSData *Data=[string dataUsingEncoding:NSUTF8StringEncoding];
    //进行编码
    Data =[GTMBase64 encodeData:Data];
    
    NSString *codestr=[[NSString alloc] initWithData:Data encoding:NSUTF8StringEncoding];
    return codestr;
}


- (NSString *)base64DecodeString:(NSString *)string
{
    NSData *Data=[string dataUsingEncoding:NSUTF8StringEncoding];
    //进行编码
    Data =[GTMBase64 decodeData:Data];
    
    NSString *codestr=[[NSString alloc] initWithData:Data encoding:NSUTF8StringEncoding];
    return codestr;
}
- (NSData *)base64Data:(NSString *)string
{
    NSData *Data=[string dataUsingEncoding:NSUTF8StringEncoding];
    //进行编码
    Data =[GTMBase64 encodeData:Data];
    return Data;
}
- (NSString *)createKeyWithLength:(int)length
{
    int passLength = length;
    //1 包括小写字符 2 包括大写字符 3 包括数字  4 包括符号
    NSString* lowercaseChars = @"abcdefghijklmnopqrstuvwxyz";
    NSString* uppercaseChars = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    NSString* numbersChars = @"0123456789";
    // NSString* punctuationChars = @”~!?#%^&*()”;
    
    
    NSString *passwordString = [NSString stringWithFormat:@"%@%@%@",lowercaseChars,uppercaseChars,numbersChars];
    
    
    NSString * password = @"";
    for (int j = 0; j < passLength; j++) {
        srand((unsigned)time(0));  //不加这句每次产生的随机数不变
        int random= arc4random() % passwordString.length;
        NSRange range = NSMakeRange(random, 1);
        NSString *passwordChar = [passwordString substringWithRange:range];
        password =[NSString stringWithFormat:@"%@%@",password,passwordChar];
    }
    
    NSLog(@"%@ \\n --------------------------------- \\n",password);
    return password;
    
}
@end
